package com.corner.presenter.imp;

import android.net.Uri;

import com.corner.app.App;
import com.corner.model.db.User;
import com.corner.presenter.INotifyItemPresenter;
import com.corner.presenter.base.BasePresenter;
import com.corner.ui.adapter.NotifyAdapter;
import com.corner.ui.view.INotifyItemView;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2015/7/15，14.22.
 * 描述：
 */
public class NotifyItemPresenter extends BasePresenter<INotifyItemView> implements INotifyItemPresenter {

    @Override
    public void requestUser(final NotifyAdapter.Holder holder, final String userId) {
        Observable.create(new Observable.OnSubscribe<User>() {
            @Override
            public void call(Subscriber<? super User> subscriber) {
                User user = App.application.daoSession.getUserDao().load(Long.valueOf(userId));
                subscriber.onNext(user);
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Action1<User>() {
            @Override
            public void call(User user) {
                if (user != null && user.getAvatar() != null && view != null) {
                    view.setAvaterUri(holder, userId, Uri.parse(user.getAvatar().getFileUrl()));
                }
            }
        });
    }
}
